package cn.itcast.geteway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * - @author by 姜暄煊
 * - @Date 11.20
 * - @Description
 */
public class AuthorizeFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求参数
        MultiValueMap<String, String> params = exchange.getRequest().getQueryParams();
        //获取authorization参数
        String auth = params.getFirst("authorization");
        //校验
        if ("admin".equals(auth)) {
            //放行
            return chain.filter(exchange);
        }
        //拦截
        //禁止访问 设置状态码
        exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
        //结束处理

        return exchange.getResponse().setComplete();
    }
}
